package com.example.service.Impl;

import com.example.config.Xmemcached;
import com.example.domain.CaptchaCase;
import com.example.service.CaptchaCaseService;
import com.example.unlits.Unlit;
import net.rubyeye.xmemcached.MemcachedClient;
import net.rubyeye.xmemcached.exception.MemcachedException;
import net.rubyeye.xmemcached.utils.XMemcachedClientFactoryBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeoutException;

@Service
public class captchaCaseServiceImpl implements CaptchaCaseService {





//    @Override
//    @CachePut(value = "codes",key = "#tole")
//    public String sendCode(String tole) {
//        return unlit.encryptionCode(tole);
//    }
//
//    @Override
//    public Boolean captchaCode(CaptchaCase captchaCase) {
//        String code = captchaCase.getCode();
//        System.out.println(code);
//        String captchaCode = unlit.get(captchaCase.getCode());
//        System.out.println(captchaCode);
//        return code.equals(captchaCode);
//    }


    // 以下由springboot未整合的Xmemcached缓存技术实现
    @Autowired
    private Unlit unlit;

    @Autowired
    private MemcachedClient xmemcached;

    @Override
    public String sendCode(String tole) throws InterruptedException, MemcachedException, TimeoutException {
        String code = unlit.encryptionCode(tole);
        System.out.print(code);
        xmemcached.set(tole,0,code);

        return code;

    }

    @Override
    public Boolean captchaCode(CaptchaCase captchaCase) throws InterruptedException, MemcachedException, TimeoutException {
        System.out.println(captchaCase.getTele());
        String code = xmemcached.get(captchaCase.getTele()).toString();
        return code.equals(captchaCase.getCode());
    }
}
